
package com.scalar.chiptrack.operations.process.actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.operations.process.dao.FabProcessSplitDAO;
import com.scalar.chiptrack.operations.process.dao.SortProcessSplitDAO;
import com.scalar.chiptrack.users.User;

public final class SortProcessSplitAction extends ChipTrackAction 
{
    public ActionForward execute (ActionMapping mapping, 
                                  ActionForm form, 
                                  HttpServletRequest request, 
                                  HttpServletResponse response) throws Exception 
	{
		HttpSession ses = request.getSession(true);
		SortProcessSplitDAO sortProcessSplitDao  = new SortProcessSplitDAO();
		FabProcessSplitDAO fabProcessSplitDao  = new FabProcessSplitDAO();

		User user = (User) ses.getAttribute (SessionKeys.USER_KEY);
		if (user == null)
		{
			return (mapping.findForward ("login"));
		}
		String [] fabProducts =(String [])ses.getAttribute("fabProduct");
		String productId=(String)ses.getAttribute("productId");
		ArrayList fabTrackList =(ArrayList)ses.getAttribute("fabTrackId");
		DBTransaction dbTransaction = null;
		boolean success = false;

		String dieBank = null;
		int noOfSorts=0;
        String pageName="SortProcessSetUp";
		String checkSort="";
		
		if( !isTokenValid(request) )
		{
			//System.out.println("not valid token sort ");
			return (mapping.findForward("sortsuccess"));
		}

		if( fabTrackList != null )
		{
			checkSort = sortProcessSplitDao.getSort((String)fabTrackList.get(0));
		}
		
		try
		{
			dbTransaction = DBTransaction.getInstance();
			dbTransaction.begin();
			if(checkSort.equals("exists"))
			{
				//System.out.println("I am here in exists");
				sortProcessSplitDao.deleteSort(productId);
			}

			for( int i=0; i < fabProducts.length ; i++ )
		    {
				dieBank = request.getParameter( "diebank" + fabProducts[i] ); 		
			    if(request.getParameterValues( fabProducts[i]) != null )
			    {              
             	  	String [] sorts = request.getParameterValues( fabProducts[i] );
					noOfSorts=new Integer(sorts.length).intValue();
			    }
				
				if( !fabProducts[i].equals("") )
				{
					int j=0;
					for( j=0; j < noOfSorts; j++ )
					{
						int no = j+1;
						String sort="Sort"+no;
				
						sortProcessSplitDao.save(dbTransaction,(String)fabTrackList.get(i),sort,"Incomplete");
					}
					if(dieBank.equals("0"))
					{
						// changed by hareesh on 6th October 2003
                        //sortProcessSplitDao.save(dbTransaction,(String)fabTrackList.get(i),"DieBank","Incomplete");
                        sortProcessSplitDao.save(dbTransaction,(String)fabTrackList.get(i),"Die Bank","Incomplete");
					}
				}
			}
			success = true;
			resetToken(request);
			saveToken(request);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{		
			if (success == true) {
            	dbTransaction.commit();
            }
            else {
            	dbTransaction.rollback();
            }
		}
    	return (mapping.findForward("sortsuccess")); 
	}
}